<?php

// @formatter:off
/**
 * @file rule.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: rule.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/content/rule.php $
 */
// @formatter:on

class ActionControllerContentRule extends WCActionController {
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcRule;

			if(!$wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You're not allowed to open rules";
			}
			if($check == WC_HTTPERROR_OK && (!$wcRule || !$wcRule->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid rule";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function subRun() {
		$ok = true;
		//
		// Loading global pointers.
		global $wcProfile;
		global $wcRule;
		global $wcRuleHooks;

		$this->_manager->PATH = "Rule '{$wcRule->name()}'";
		//
		// Creating multi-array by prefix for all rule actions.
		$lists = array();
		$ids = 1;
		$actions = $wcRule->actions();
		uasort($actions, "WCRuleAction::NameSort");
		foreach($actions as $action) {
			$name = $action->name();
			$prefix = strtoupper(isset($name[0]) ? $name[0] : "#");
			if($prefix < 'A' || $prefix > 'Z') {
				$prefix = '#';
			}
			$prefix = "{$prefix}";
			if(!isset($lists[$prefix])) {
				$lists[$prefix] = array(
					"prefix" => $prefix,
					"list" => array()
				);
			}

			$lists[$prefix]["list"][] = WCFVBoxes::GetRuleActionPreviewAssigns($action);
		}
		ksort($lists);
		$this->_manager->actions_lists = $lists;
		//
		// Creating a list of action that can be created.
		$list = array();
		foreach($wcRuleHooks as $type => $hook) {
			$list[] = array(
				"type" => $type,
				"name" => $hook["name"],
				"creator" => "rule:".strtolower($type).":create"
			);
		}
		$this->_manager->actions_types = $list;

		if($wcProfile->allowedTo(WC_PERM_RULE_RENAME)) {
			$this->_manager->info_name_editable = true;
		}

		WCViewInfoFactory::LoadInfo($wcRule, true);
		WCViewInfoFactory::LoadInterfacesInfo($wcRule, true);

		return $ok;
	}
}
